package com.whenjun.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.crypto.digest.MD5;
import com.ifootbook.util.MD5Util;
import com.ifootbook.util.common.BackJson;
import com.ifootbook.util.common.CallResult;
import com.whenjun.common.LoggerParent;
import com.whenjun.dto.login.BaseLogin;
import com.whenjun.dto.login.LoginResp;
import com.whenjun.dto.login.PasswordUpdate;
import com.whenjun.dto.login.TokenInfo;
import com.whenjun.service.core.LoginService;
import com.whenjun.service.impl.AccessLogServiceImpl;
import com.whenjun.service.impl.AccountServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/")
@Api(tags = "登陆、注册")
public class LoginController extends LoggerParent {

    @Autowired
    private LoginService loginService;

    @Autowired
    private AccountServiceImpl accountService;

    @PostMapping("login")
    @ApiOperation("登陆")
    public CallResult<TokenInfo> login(@Valid @RequestBody BaseLogin baseLogin){
        return BackJson.successObj(loginService.login(baseLogin));
    }

    @ApiOperation("用户信息")
    @PostMapping("userInfo")
    @SaCheckLogin
    @ApiImplicitParams({
            @ApiImplicitParam(name="token",value="token",paramType="header",required=false,dataType="String",dataTypeClass = String.class),
    })
    public CallResult<LoginResp> userInfo(){
        return BackJson.successObj(loginService.userInfo());
    }


    @ApiOperation("登出")
    @PostMapping("logout")
    @SaCheckLogin
    @ApiImplicitParams({
            @ApiImplicitParam(name="token",value="token",paramType="header",required=false,dataType="String",dataTypeClass = String.class),
    })
    public CallResult<?> logout(HttpServletRequest request){
        loginService.logout(request);
        return BackJson.success();
    }

    @ApiOperation("修改登陆密码")
    @PostMapping("updatePassword")
    @SaCheckLogin
    @ApiImplicitParams({
            @ApiImplicitParam(name="token",value="token",paramType="header",required=false,dataType="String",dataTypeClass = String.class),
    })
    public CallResult<?> updatePassword(@Valid @RequestBody PasswordUpdate passwordUpdate){
        accountService.updatePassword(passwordUpdate);
        return BackJson.success();
    }

}
